package net.scrumtool.controller;

import java.util.List;

import net.scrumtool.model.Collaborator;
import net.scrumtool.model.Product;
import net.scrumtool.model.Sprint;
import net.scrumtool.model.Status;
import net.scrumtool.model.Userstory;
import net.scrumtool.technics.DAO;
import net.scrumtool.technics.Utils;
import net.scrumtool.view.OverviewProduct;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.MessageBox;

public class CoverviewProductBacklog {

	OverviewProduct view;
	private Userstory userstoryActif;

	public CoverviewProductBacklog(OverviewProduct view) {
		this.view = view;
	}

	public void init(Collaborator collaborator, Product productActif, Userstory userstory) {
		view.getTltmAddUserstory().setEnabled(true);
		view.getTltmModifieUserstory().setEnabled(false);
		view.getTltmRemoveUserStory().setEnabled(false);
	}

	public void listener(Collaborator collaborator, final Product productActif) {

		view.getTableBacklog().addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {

				StructuredSelection sel = (StructuredSelection) view.getTableViewerBacklog().getSelection();
				setUserStoryActif((Userstory) sel.getFirstElement());

				view.getTltmAddUserstory().setEnabled(true);
				view.getTltmModifieUserstory().setEnabled(true);
				view.getTltmRemoveUserStory().setEnabled(true);

			}

		});

		view.getTltmModifieUserstory().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				view.getBtnValider().setImage(Utils.getImage(Utils.IMG_EDIT));
				view.getBtnValider().setText("Save this user story");
				view.getBtnValider().setVisible(true);
				view.getTxtNom().setText(userstoryActif.getLabel());
				view.getTxtDescription().setText(userstoryActif.getDescription());
				view.getComposite_6().setEnabled(true);

				List<Sprint> sprints = DAO.getSprintsByNameProduct(productActif.getName());
				view.getComboViewerSprints().setContentProvider(new ArrayContentProvider());
				view.getComboViewerSprints().setInput(sprints);
				view.getComboViewerSprints().setSelection(new StructuredSelection(userstoryActif.getSprint()));

				view.getTxtPtAwarded().setText(userstoryActif.getStoryPoints().toString());
				view.getTxtPriority().setText(userstoryActif.getPriority().toString());

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTltmAddUserstory().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				view.getTltmModifieUserstory().setEnabled(false);
				view.getTltmRemoveUserStory().setEnabled(false);
				view.getTxtNom().setText("");
				view.getTxtDescription().setText("");
				view.getTxtPtAwarded().setText("");
				view.getTxtPriority().setText("");
				view.getComposite_6().setEnabled(true);

				List<Sprint> sprints = DAO.getSprintsByNameProduct(productActif.getName());
				view.getComboViewerSprints().setContentProvider(new ArrayContentProvider());
				view.getComboViewerSprints().setInput(sprints);
				view.getComboViewerSprints().setSelection(new StructuredSelection());

				view.getBtnValider().setImage(Utils.getImage(Utils.IMG_ADD));
				view.getBtnValider().setText("Add this userstory");
				view.getBtnValider().setVisible(true);

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getTltmRemoveUserStory().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				MessageBox messageBoxUserstory = new MessageBox(view.getShell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
				messageBoxUserstory.setText("SCRUMTOOL");
				messageBoxUserstory.setMessage("Do you really want to delete this userstory ?");
				int responseUserstory = messageBoxUserstory.open();
				if (responseUserstory == SWT.YES) {
					DAO.SQLUserStory(userstoryActif, "delete");
					view.getTableViewerBacklog().remove(userstoryActif);

					if (userstoryActif.getStatus().getIdStatus() == 1) {
						view.getTableViewerToDoOverview().remove(userstoryActif);
					} else if (userstoryActif.getStatus().getIdStatus() == 2) {
						view.getTableViewerInProgressOverview().remove(userstoryActif);
					} else {
						view.getTableViewerDoneOverview().remove(userstoryActif);
					}

					view.getTltmAddUserstory().setEnabled(false);
					view.getTltmModifieUserstory().setEnabled(false);
					view.getTltmRemoveUserStory().setEnabled(false);
					view.getBtnValider().setVisible(false);
					view.getTxtDescription().setText("");
					view.getTxtNom().setText("");
					view.getComboViewerSprints().getCombo().removeAll();
					view.getTxtPtAwarded().setText("");
					view.getTxtPriority().setText("");
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		view.getBtnValider().addSelectionListener(new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if (view.getBtnValider().getText() == "Add this userstory") {
					if (view.getTxtNom().getText().equals("") || view.getTxtDescription().getText().equals("") || view.getTxtPtAwarded().getText().equals("") || view.getTxtPriority().getText().equals("")) {
						Utils.MsgBox(view.getShell(), "Please complete all fields", "information");
					} else {
					
							IStructuredSelection sel = (IStructuredSelection) (view.getComboViewerSprints().getSelection());
							Sprint sprint = (Sprint) (sel.iterator().next());
							Status status=new Status();
							if(view.getTableViewerColumnStatus().getColumn().getText()=="To do"){
								
								status.setIdStatus(1);
								status.setLabel("To do");
								status.setUserstories(null);
							}else if(view.getTableViewerColumnStatus().getColumn().getText()=="In progress"){
								status.setIdStatus(2);
								status.setLabel("In progress");
								status.setUserstories(null);
							}else{
								status.setIdStatus(3);
								status.setLabel("Done");
								status.setUserstories(null);
							}
							
							Userstory userstory = new Userstory(sprint, productActif, status, view.getTxtNom().getText(), view.getTxtDescription().getText(), Integer.parseInt(view.getTxtPtAwarded().getText()), Integer.parseInt(view.getTxtPriority().getText()), null);
							DAO.SQLUserStory(userstory, "insert");
							view.getTableViewerBacklog().add(userstory);
							Utils.MsgBox(view.getShell(), "Userstory is create", "information");
						}

					

				} else {
					userstoryActif.setLabel(view.getTxtNom().getText());
					userstoryActif.setDescription(view.getTxtDescription().getText());
					userstoryActif.setStoryPoints(Integer.parseInt(view.getTxtPtAwarded().getText()));
					userstoryActif.setPriority(Integer.parseInt(view.getTxtPriority().getText()));
					IStructuredSelection sel = (IStructuredSelection) (view.getComboViewerSprints().getSelection());
					Sprint sprint = (Sprint) (sel.iterator().next());
					userstoryActif.setSprint(sprint);
					DAO.SQLUserStory(userstoryActif, "update");
					view.getTableViewerBacklog().refresh();
					view.getTableViewerToDoOverview().refresh();
					view.getTableViewerInProgressOverview().refresh();
					view.getTableViewerDoneOverview().refresh();
					view.getComposite_6().setEnabled(false);
				}

			}

			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void setUserStoryActif(Userstory userstoryActif) {
		this.userstoryActif = userstoryActif;
	}

}
